//
//  JMKeyInforController.m
//  OpenSesame
//
//  Created by zhangjiaming on 15/12/25.
//  Copyright © 2015年 OpenSesame. All rights reserved.
//

#import "JMKeyInforController.h"
#import "JMDetailView.h"
#import "JMUserAccountTool.h"
#import "MBProgressHUD+JM.h"
#import "JMRemarkResult.h"
#import "JMFrequencyViewController.h"

@interface JMKeyInforController () <JMDetailViewDelegate>
@property (weak, nonatomic) UIScrollView *scroll;
@property (weak, nonatomic) UIButton *generateBtn; // 生成按钮
@property (weak, nonatomic) UIButton *copysBtn; // 拷贝按钮
@property (weak, nonatomic) UIButton *testBtn; // 测试按钮
@property (weak, nonatomic) UIButton *bluetoothBtn;
@property (weak, nonatomic) JMDetailView *detailView;
@end

@implementation JMKeyInforController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = _nameStr;
    // 设置界面
    [self setUpInterface];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  设置界面
 */
- (void)setUpInterface {
    [self obtainRemark];
    // 设置导航栏右侧内容
    
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 60, 44);
    [btn setImage:[UIImage imageNamed:@"蓝牙 未连"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"蓝牙 按下"] forState:UIControlStateHighlighted];
    [btn setImage:[UIImage imageNamed:@"蓝牙"] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(settingBlueTooth) forControlEvents:UIControlEventTouchUpInside];
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -25);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.bluetoothBtn = btn;
    
    // 设置ScrollView
    UIScrollView *scroll = [[UIScrollView alloc] init];
    self.scroll = scroll;
    scroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:scroll];
    scroll.frame = CGRectMake(0, 0, JMPhoneWidth, JMPhoneHeight);
    self.scroll.showsVerticalScrollIndicator = NO;
    
    // 设置顶部topImageView
    UIImageView *topImageView = [[UIImageView alloc] init];
    [scroll addSubview:topImageView];
    topImageView.image = [UIImage imageNamed:@"详情图"];
    CGFloat topRatio = 750.0/304.0; // 图片宽高比
    topImageView.width = scroll.width;
    topImageView.height = topImageView.width/topRatio;
    topImageView.y = -1;
    topImageView.x = 0;
    
    // 7个高度的背景
    UIView *backVIew = [[UIView alloc] init];
    backVIew.frame = CGRectMake(0, topImageView.y + topImageView.height, JMPhoneWidth, 7);
    backVIew.backgroundColor = JMColor(242, 242, 242);
    [scroll addSubview:backVIew];
    
    // 添加deatilView
    JMDetailView *deatilView = [[JMDetailView alloc] initWithFrame:CGRectMake(0, backVIew.y + backVIew.height*2, JMPhoneWidth, JMPhoneHeight)];
    deatilView.delegate = self;
    [self.scroll addSubview:deatilView];
    deatilView.frequencyLabel.text =  _dateStr    ;// @"315MHz";
    deatilView.fitLabel.text =  _fitStr;
    deatilView.optionalLabel.text = @"可选功能";
    deatilView.illustrateLabel.text = _contentStr;
    self.detailView = deatilView;
    
    // 添加最下面四个按钮
    [self addButtons];
    self.scroll.contentSize = CGSizeMake(0, deatilView.illustrateLabel.y + deatilView.illustrateLabel.height + deatilView.y+ 50 + 64);
}

/** 获取个人备注 */
- (void)obtainRemark {
    // 获取时间戳
    NSString *timeStamp = [NSDate timeStamp];
    
    // 获取用户信息
    JMUserAccount *account = [JMUserAccountTool account];
    
    // 设置请求参数
    NSMutableArray *params = [[NSMutableArray alloc] initWithObjects:@"USER_GET_BEIZHU",@"ID",account.ID,@"ModelID",@"321",@"Time",timeStamp,@"Token",account.Token, nil];
    
    // 发送请求
    [JMHttpTool requestWithParameter:params success:^(JMParam *result) {
        if ([result.Result isEqualToString:@"0"]) { // 成功
            self.detailView.commentTextView.text = result.Beizhu;
        } else if ([result.Result isEqualToString:@"1"]) { // 服务器出错
            [MBProgressHUD showError:@"服务器出错"];
        } else if ([result.Result isEqualToString:@"2"]) { // 错误的型号
            [MBProgressHUD showError:@"错误的型号"];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"请检查您的网络"];
    }];
}

- (void)addButtons{
    CGFloat ratio = 2/1.0; // 图片宽高比
    CGFloat btnW = (JMPhoneWidth - 50)/4;
    CGFloat btnH = btnW/ratio;
    CGFloat btnY = JMPhoneHeight - btnH - 10;
    
    // 四个按钮的背景框
    UIView *view1 = [[UIView alloc] init]; // 上
    view1.frame = CGRectMake(0, btnY - 10, JMPhoneWidth, 1);
    view1.backgroundColor = JMColor(242, 242, 242);
    [self.view addSubview:view1];
    
    UIView *view3 = [[UIView alloc] init]; // 左
    view3.frame = CGRectMake(0, view1.y, 1, JMPhoneHeight - view1.y);
    view3.backgroundColor = JMColor(242, 242, 242);
    [self.view addSubview:view3];
    
    UIView *view4 = [[UIView alloc] init]; // 右
    view4.frame = CGRectMake(JMPhoneWidth - 1, view1.y, 1, JMPhoneHeight - view1.y);
    view4.backgroundColor = JMColor(242, 242, 242);
    [self.view addSubview:view4];
    
    // 拷贝按钮
    UIButton *copyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [copyBtn addTarget:self action:@selector(jumpToFrequency) forControlEvents:UIControlEventTouchUpInside];
    self.copysBtn = copyBtn;
    [copyBtn setBackgroundImage:[UIImage imageNamed:@"copybtn0"] forState:UIControlStateNormal];
    [copyBtn setBackgroundImage:[UIImage imageNamed:@"copybtn1"] forState:UIControlStateHighlighted];
    copyBtn.frame = CGRectMake(10, btnY, btnW, btnH);
    
    // 生成按钮
    UIButton *generateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [generateBtn addTarget:self action:@selector(jumpToFrequency) forControlEvents:UIControlEventTouchUpInside];
    self.generateBtn = generateBtn;
    [generateBtn setBackgroundImage:[UIImage imageNamed:@"genbtn0"] forState:UIControlStateNormal];
    [generateBtn setBackgroundImage:[UIImage imageNamed:@"genbtn1"] forState:UIControlStateHighlighted];
    generateBtn.frame = CGRectMake(20 + btnW, btnY, btnW, btnH);
    
    // 测试按钮
    UIButton *testBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [testBtn addTarget:self action:@selector(jumpToFrequency) forControlEvents:UIControlEventTouchUpInside];
    self.testBtn = testBtn;
    [testBtn setBackgroundImage:[UIImage imageNamed:@"testbtn0"] forState:UIControlStateNormal];
    [testBtn setBackgroundImage:[UIImage imageNamed:@"testbtn1"] forState:UIControlStateHighlighted];
    testBtn.frame = CGRectMake(30 + 2*btnW, btnY, btnW, btnH);
    
    // 对比按钮
    UIButton *compareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [compareBtn addTarget:self action:@selector(jumpToFrequency) forControlEvents:UIControlEventTouchUpInside];
    [compareBtn setBackgroundImage:[UIImage imageNamed:@"checkbtn0"] forState:UIControlStateNormal];
    [compareBtn setBackgroundImage:[UIImage imageNamed:@"checkbtn1"] forState:UIControlStateHighlighted];
    compareBtn.frame = CGRectMake(40 + 3*btnW, btnY, btnW, btnH);
    [self.view addSubview:copyBtn];
    [self.view addSubview:generateBtn];
    [self.view addSubview:testBtn];
    [self.view addSubview:compareBtn];
}

- (void)jumpToFrequency {
    [self.navigationController pushViewController:[[JMFrequencyViewController alloc] init] animated:YES];
}

/**
 *  设置蓝牙
 */
- (void)settingBlueTooth {
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:url];
    }
}

#pragma mark - JMDetailViewDelegate
-(void)detailView:(JMDetailView *)detailView moveUpTo:(float)distance {
    [UIView animateWithDuration:0.3 animations:^{
        self.view.y = distance - 64;;
    }];
}

/**
 *  发送请求添加个人备注
 */
- (void)detailView:(JMDetailView *)detailView moveDownTo:(float)distance andRemark:(NSString *)remark{
    [UIView animateWithDuration:0.3 animations:^{
        self.view.y = distance;
    }];
    
    // 获取时间戳
    NSString *timeStamp = [NSDate timeStamp];
    
    // 获取用户信息
    JMUserAccount *account = [JMUserAccountTool account];
    
    // 设置请求参数
    NSMutableArray *params = [[NSMutableArray alloc] initWithObjects:@"USER_POST_BEIZHU",@"ID",account.ID,@"ModelID",@"321",@"Beizhu",remark,@"Time",timeStamp,@"Token",account.Token, nil];
    
    // 发送请求
    [JMHttpTool requestWithParameter:params success:^(JMParam *result) {
        if ([result.Result isEqualToString:@"0"]) { // 成功
            self.detailView.commentTextView.text = result.Beizhu;
        } else if ([result.Result isEqualToString:@"1"]) { // 服务器出错
            [MBProgressHUD showError:@"服务器出错"];
        } else if ([result.Result isEqualToString:@"2"]) { // 错误的型号
            [MBProgressHUD showError:@"错误的型号"];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"请检查您的网络"];
    }];
}
@end
